#!/usr/bin/env python
import pygame
import lwpgt
import sys
import pygame

SCREEN=pygame.display.set_mode((640,480))
#FIXME: create new clean map
##game class
class MapEditor:
    def __init__(self):
        """init MapEditor"""
        #load tiles database
        self.DB=lwpgt.tools.ZipDB("data/tiles.zip")
        self.mainmap=lwpgt.render2d.Maps(100, 100, 3)
        self.camera=lwpgt.render2d.Camera(pygame.Rect(0, 0, 640, 400))
        self.tiles=self.loadtiles()
        #self.mainmap.LoadMapFromXML("testmap.xml", self.tiles)
        #gui
        self.guicontainer=lwpgt.gui.Container()
        self.backbutton=lwpgt.gui.TextBox(text="Previous", size=(100, 22), position=(0, 460), editable=False)  
        self.nextbutton=lwpgt.gui.TextBox(text="Next", size=(80, 22), position=(100, 460), editable=False)
        self.savebutton=lwpgt.gui.TextBox(text="Save", size=(80, 22), position=(560, 460), editable=False, color=(255, 128, 255))
        self.nextbutton.Connect(lwpgt.EVT_ONCLICK, self.onnext_handler)
        self.backbutton.Connect(lwpgt.EVT_ONCLICK, self.onback_handler)
        self.savebutton.Connect(lwpgt.EVT_ONCLICK, self.onsave_handler)
        self.guicontainer.Add(self.nextbutton)
        self.guicontainer.Add(self.backbutton)
        self.guicontainer.Add(self.savebutton)
        #others
        self.drag=False
        self.selectedtile=1
        #set caption
        pygame.display.set_caption("Render2D Map Editor")
        
    def update(self):
        """update the map editor"""
        pass
    
    def redraw(self,screen):
        """redraw screen"""
        screen.fill((0,0,0))
        self.mainmap.Render(screen, self.camera)
        pygame.draw.line(screen, (255, 255, 255), (0, 400), (640, 400))
        self.guicontainer.Redraw(screen)
        pygame.display.flip()
        
    def events(self):
        """events handler"""
        for e in pygame.event.get():
            self.guicontainer.PollEvent(e)
            if e.type==pygame.QUIT:
                sys.exit()
            #mouse buttons
            if e.type==pygame.MOUSEBUTTONDOWN:
                #get mouse buttons state
                mbs=pygame.mouse.get_pressed()
                #left mouse button
                if mbs[0]==1:
                    #get mouse position
                    mx, my=pygame.mouse.get_pos()
                    #get tile coord from mouse position including camera
                    tilepos=self.mainmap.GetMapCoordinates((mx, my), self.camera)
                    if tilepos!=None:
                        tilestatus=self.mainmap.GetTile(tilepos[0],tilepos[1], 1)
                        #if tile is free drop tile
                        if tilestatus==None:
                            self.mainmap.SetTile(tilepos[0],tilepos[1], 1, self.tiles[self.selectedtile])
                        else:
                            self.mainmap.SetTile(tilepos[0],tilepos[1], 1, None)
                #middle mouse button
                if mbs[1]==1:
                    pass
                #right mouse button
                if mbs[2]==1:
                    #set drag
                    self.drag=True

            if e.type==pygame.MOUSEBUTTONUP:
                self.drag=False
                
            #mouse moveing
            if e.type==pygame.MOUSEMOTION:
                if self.drag:self.camera.MoveCamera(e.rel[0], e.rel[1])
        
    def mainloop(self):
        """mainloop for mapeditor"""
        while 1:
            self.events()
            self.update()
            self.redraw(SCREEN)
    
    def loadtiles(self):
        """load tiles from DB"""
        tiles=[]
        tiles.append(lwpgt.render2d.Tile(self.DB.GetImage("tile.bmp"), static=True))
        tiles.append(lwpgt.render2d.Tile(self.DB.GetImage("tile1.bmp"), static=True))
        tiles.append(lwpgt.render2d.Tile(self.DB.GetImage("hero.bmp"), static=True))
        return tiles
    
    def onnext_handler(self, widget):
        """on next handler"""
        self.selectedtile+=1
        if self.selectedtile>len(self.tiles)-1:
            self.selectedtile=len(self.tiles)-1
    
    def onback_handler(self, widget):
        self.selectedtile-=1
        if self.selectedtile<0:
            self.selectedtile=0
    
    def onsave_handler(self, widget):
        self.mainmap.SaveMapToXML("mapka.txt", self.tiles)

def run():
    """run the mapEditor"""
    MapEditor().mainloop()
